package common.utils
{
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.formatters.DateFormatter;
	import mx.modules.Module;

	public class StringUtil
	{
		public static function isBlank(str:String):Boolean {
			if (str == null || str == "")
			{
				return true;
			} else {
				return false;
			}
		}

		public static function isEquals(str1:String,str2:String):Boolean {
			if (isBlank(str1) && isBlank(str2) ) {
				return true;
			} else {
				return (str1 == str2);
			}
		}

		public static function toInt(str:String):int {
			var ires:int = 0;
			if (isBlank(str) ) {
				return ires;
			}
			try {
				ires = int(str);
			} catch(e:Error) {
				ires = 0;
			}
			return ires;
		}

		public static function toNumber(str:String):int {
			if (isBlank(str)) {
				return 0;
			}
			var nres:Number = 0;
			try {
				nres = parseFloat(str);
			} catch(e:Error) {
				nres = 0;
			}
			return nres;
		}

		public static function parseParamter(module:Module):ArrayList {
			try {
				// Remove everything before the question mark, including
				// the question mark.
				var myPattern:RegExp = /.*\?/;
				var s:String = module.loaderInfo.url.toString();
				var index:int = s.indexOf("?");
				var paramText:String = s.substr(index+1);
				var params:Array = paramText.split("&");
				var resList:ArrayList = new ArrayList();
				if (params.length == 0) {
					resList.addItem(paramText);
				} else {
					for (var i:int=0;i<params.length;i++) {
						resList.addItem(params[i].toString());
					}
				}
				return resList;

			} catch (e:Error) {
				Alert.show(e.toString());
			}
			return null;
		}

		public static function getParameter(paramName:String,paramList:ArrayList):String {
			var res:String = "";
			for (var i:int=0;i<paramList.length;i++) {
				var paramText:String = paramList.getItemAt(i).toString();
				var index:int = paramText.indexOf("=");
				var preText:String = paramText.substr(0,index);
				var lastText:String = paramText.substr(index+1);
				if (preText == paramName) {
					res = lastText;
					break;
				}
			}
			return res;
		}

		public static function formatGridDate(item:Object,column:DataGridColumn,dfComp:DateFormatter):String {
			return dfComp.format(item);// df是一个DateFormatter，这里格式化item数据中的startDate字段
		}

	}
}